//
//  QueryResultTableViewController.m
//  Express
//
//  Created by lanou on 16/1/9.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "QueryResultTableViewController.h"
#import "BaseHeader.h"

@interface QueryResultTableViewController ()

@property (nonatomic, strong) NSMutableArray *logosticsQueryTimeResult;
@property (nonatomic, strong) NSMutableArray *logosticsQueryMessageResult;
@end

@implementation QueryResultTableViewController

- (NSMutableArray *)logosticsQueryTimeResult {
    if (!_logosticsQueryTimeResult) {
        self.logosticsQueryTimeResult = [[NSMutableArray alloc] init];
    }
    return _logosticsQueryTimeResult;
}

- (NSMutableArray *)logosticsQueryMessageResult {
    if (!_logosticsQueryMessageResult) {
        self.logosticsQueryMessageResult = [[NSMutableArray alloc] init];
    }
    return _logosticsQueryMessageResult;
}

- (NSString *)expressStatusJudgeWithNmber:(NSNumber *)number {
    NSInteger status = [number integerValue];
    NSString *statusString;
    switch (status) {
        case 1:
            statusString = @"物流单号暂无结果";
            break;
        case 3:
            statusString = @"在途中";
            break;
        case 4:
            statusString = @"正在揽件";
            break;
        case 5:
            statusString = @"疑难";
            break;
        case 6:
            statusString = @"已签收";
            break;
        case 7:
            statusString = @"已退签";
            break;
        case 8:
            statusString = @"派件中";
            break;
        case 9:
            statusString = @"已退回";
            break;
        default:
            statusString = @"暂无";
            break;
    }
    return statusString;
}

- (void)jsonWithLogosticsQueryUrl {
    [LORequestManger GET:[NSString stringWithFormat:kLogosticsQueryURl,self.express.expressCode,self.express.expressOrderNumber] success:^(id response) {
        NSLog(@"%@",[NSString stringWithFormat:kLogosticsQueryURl,self.express.expressCode,self.express.expressOrderNumber]);
        NSDictionary *dictionary = (NSDictionary *)response;
        NSLog(@"%@",dictionary[@"success"]);

        if ([dictionary[@"success"] isEqualToNumber:@1]) {
            for (NSDictionary *dic in dictionary[@"data"]) {
                [self.logosticsQueryTimeResult addObject:dic[@"time"]];
                [self.logosticsQueryMessageResult addObject:dic[@"context"]];
            }
            _express.expressStatus = [self expressStatusJudgeWithNmber:dictionary[@"status"]];
            [[HistoryDataBase shareHistoryDataBase] updateFromCloudDBWithModel:_express];
            
            if ([self.express.objectId isEqualToString:@""]) {
                [[HistoryDataBase shareHistoryDataBase] insertSingleDataToCloudDBWithModel:_express];
            } else {
                NSLog(@"is exist");
            }
            
        } else {
            [self.logosticsQueryTimeResult addObject:dictionary[@"reason"]];
        }
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.logosticsQueryMessageResult removeAllObjects];
    [self.logosticsQueryTimeResult removeAllObjects];
    [self jsonWithLogosticsQueryUrl];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.logosticsQueryTimeResult.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    if (self.logosticsQueryMessageResult.count > 0) {
        cell.textLabel.text = self.logosticsQueryMessageResult[indexPath.row];
    } else {
        cell.textLabel.text = self.logosticsQueryTimeResult[indexPath.row];
    }
    return cell;
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
